﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace SupplyChainManagement
{
    /// <summary>
    /// Form to specify the amount of raw items to order
    /// </summary>
    public partial class OrderInventoryNext : Form
    {
        OrderInventory orderForm;
        InventoryForm main;
        List<TextBox> textboxes = new List<TextBox>();
        List<string> selectedItems;

        public OrderInventoryNext(List<string> selectedItems, OrderInventory orderForm, InventoryForm main)
        {
            this.orderForm = orderForm;
            this.main = main;
            this.selectedItems = selectedItems;
            InitializeComponent();
            //create the textboxes and labels
            foreach (string s in selectedItems)
            {
                Label l = new Label();
                l.Text = s;
                TextBox t = new TextBox();
                layoutPanel.Controls.Add(l);
                layoutPanel.Controls.Add(t);
                textboxes.Add(t);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SaveToDatabase();
            this.Close();
            orderForm.Close();
            main.Enabled = true;
            main.LoadDatabase();
            main.LoadDatabase();
            main.Activate();
        }

        private void SaveToDatabase()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                dbMySQL.Open();
                int txtIndex = 0;
                foreach (string s in selectedItems)
                {
                    OdbcCommand rawItemCmd = dbMySQL.CreateCommand();
                    rawItemCmd.CommandText = "select * from rawitem";

                    OdbcDataReader rawReader = rawItemCmd.ExecuteReader();
                    int index = 1;
                    string inv_id = null;
                    string last_id = null;
                    //to get the last index, for the purpose of primary key auto increment
                    while (rawReader.Read())
                    {
                        last_id = rawReader.GetString(0);
                    }
                    index = int.Parse(last_id) + 1;
                    rawItemCmd.Dispose();
                    //get the inventory id
                    rawItemCmd = dbMySQL.CreateCommand();
                    rawItemCmd.CommandText = "select * from rawitem where inv_name='" + s + "'";
                    OdbcDataReader rawIdReader = rawItemCmd.ExecuteReader();
                    while (rawIdReader.Read())
                    {
                        inv_id = rawIdReader.GetString(1);
                    }
                    int amount = int.Parse(textboxes[txtIndex].Text);
                    //insert the raw items
                    for (int i = 0; i < amount; i++)
                    {
                        OdbcCommand rawItemInsert = dbMySQL.CreateCommand();
                        rawItemInsert.CommandText = "insert into rawitem value ('" + index + "','" + inv_id + "','" +
                        s + "','ready')";
                        rawItemInsert.ExecuteNonQuery();
                        rawItemInsert.Dispose();
                        index++;
                    }
                    txtIndex++;
                    rawReader.Dispose();
                }
                MessageBox.Show("Success!");
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
            orderForm.Enabled = true;
            orderForm.Activate();
        }

        private void OrderInventoryNext_FormClosed(object sender, FormClosedEventArgs e)
        {
            orderForm.Enabled = true;
            orderForm.Activate();
        }
    }
}
